package qldc.pm.tienan.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qldc.pm.qtht.dmdc.model.QuanHeGiaDinh;
import qldc.pm.qtht.dmdc.service.QuanHeGiaDinhLocalServiceUtil;
import qldc.pm.qtht.dmdc.utils.DiaChiUtils;
import qldc.pm.tienan.NoSuchDoiTuongTienAnException;
import qldc.pm.tienan.NoSuchLichSuXoaAnTichException;
import qldc.pm.tienan.constants.TienAnConstants;
import qldc.pm.tienan.constants.TrangThaiTienAnEnum;
import qldc.pm.tienan.constants.TrangThaiTienAnTrongThongKeEnum;
import qldc.pm.tienan.dto.DS_HoGiaDinhDTO;
import qldc.pm.tienan.dto.DS_LichSuPhamToiDTO;
import qldc.pm.tienan.dto.DS_NguoiTienAnDTO;
import qldc.pm.tienan.dto.DS_TaiPhamDTO;
import qldc.pm.tienan.dto.DS_XoaAnTichDTO;
import qldc.pm.tienan.dto.HoGiaDinhDTO;
import qldc.pm.tienan.dto.SoHoKhauDTO;
import qldc.pm.tienan.model.DoiTuongTienAn;
import qldc.pm.tienan.model.TienAn;
import qldc.pm.tienan.service.DoiTuongTienAnLocalServiceUtil;
import qldc.pm.tienan.service.LichSuXoaAnTichLocalServiceUtil;
import qldc.pm.tienan.service.TienAnLocalServiceUtil;
import qldc.pm.tienan.service.base.DoiTuongTienAnLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;

/**
 * The implementation of the doi tuong tien an local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link qldc.pm.tienan.service.DoiTuongTienAnLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tienan.service.base.DoiTuongTienAnLocalServiceBaseImpl
 * @see qldc.pm.tienan.service.DoiTuongTienAnLocalServiceUtil
 */
public class DoiTuongTienAnLocalServiceImpl extends
		DoiTuongTienAnLocalServiceBaseImpl {
	private static Log _log = LogFactoryUtil
			.getLog(DoiTuongTienAnLocalServiceImpl.class);
	private SimpleDateFormat sdf = new SimpleDateFormat(TienAnConstants.DATE_PATTERN);

	public DoiTuongTienAn findByDoiTuongTienAnId(long doiTuongTienAnId) {
		DoiTuongTienAn doiTuongTienAn = null;

		try {
			doiTuongTienAn = doiTuongTienAnPersistence
					.findByPrimaryKey(doiTuongTienAnId);
		} catch (NoSuchDoiTuongTienAnException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return doiTuongTienAn;
	}
	public TienAn layTienAnDangThiHanhAnCuaDoiTuong(String soDinhDanhCaNhan){
		try {
			DoiTuongTienAn doiTuong = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

			if (Validator.isNotNull(doiTuong)){
				List<TienAn> list = tienAnPersistence.findByDoiTuongTienAnId(doiTuong.getDoiTuongTienAnId());
				for (TienAn ts : list){
					TrangThaiTienAnEnum tr = TienAnLocalServiceUtil.kiemTraTrangThaiCuaTienAn(ts.getTienAnId());
					if (tr.getValue() == TrangThaiTienAnEnum.DANG_THI_HANH_AN.getValue() || tr.getValue() == TrangThaiTienAnEnum.DANG_LAP_YEU_CAU_XOA_AN_TICH.getValue()  ) {
						return ts;
					}
				}
			}
		}
		 catch (NoSuchLichSuXoaAnTichException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
		} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
		}
		return null;
	}
	public List<TienAn> findTienAnBySoSoHoKhau(String soSoHoKhau) throws SystemException{
		return doiTuongTienAnFinder.findTienAnBySoSoHoKhau(soSoHoKhau);
	}
	public List<TienAn> layDanhSachTienAnDangThiHanhAnCuaDoiTuong(String soDinhDanhCaNhan){
		List<TienAn> tienAns = new ArrayList<TienAn>();
		try {
			DoiTuongTienAn doiTuong = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

			if (Validator.isNotNull(doiTuong)){
				List<TienAn> list = tienAnPersistence.findByDoiTuongTienAnId(doiTuong.getDoiTuongTienAnId());
				for (TienAn ts : list){
					TrangThaiTienAnEnum tr = TienAnLocalServiceUtil.kiemTraTrangThaiCuaTienAn(ts.getTienAnId());
					if (tr.getValue() == TrangThaiTienAnEnum.DANG_THI_HANH_AN.getValue() || tr.getValue() == TrangThaiTienAnEnum.DANG_LAP_YEU_CAU_XOA_AN_TICH.getValue()  ) {
						tienAns.add(ts);
					}
				}
			}
		}
		 catch (NoSuchLichSuXoaAnTichException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return new ArrayList<TienAn>();
		} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return new ArrayList<TienAn>();
		}
		return tienAns;
	}

	public DoiTuongTienAn findBySoDinhDanhCaNhan(String soDinhDanhCaNhan) {
		DoiTuongTienAn doiTuongTienAn = null;

		try {
			doiTuongTienAn = doiTuongTienAnPersistence
					.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
		} catch (NoSuchDoiTuongTienAnException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return doiTuongTienAn;
	}

	public DoiTuongTienAn addDoiTuongTienAn(User user, String soDinhDanhCaNhan)
			throws SystemException {
		long doiTuongTienAnId = counterLocalService.increment(DoiTuongTienAn.class.getName());
		DoiTuongTienAn doiTuongTienAn = createDoiTuongTienAn(doiTuongTienAnId);

		doiTuongTienAn.setUserId(user.getUserId());
		doiTuongTienAn.setUserName(user.getScreenName());
		doiTuongTienAn.setCreateDate(new Date());

		doiTuongTienAn.setSoDinhDanhCaNhan(soDinhDanhCaNhan);

		doiTuongTienAn = addDoiTuongTienAn(doiTuongTienAn);

		return doiTuongTienAn;
	}


	public int countBySDDCN_CMND_HVT_SSHK_DCTT(String soDinhDanhCaNhan,
			String soCMND, String hoVaTen, String soSoHoKhau, long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi,String banAnSo, Date tuNgayToaAn, Date denNgayToaAn, int trangThai) {
		int count = 0;
		try {
			count = doiTuongTienAnFinder.countBySDDCN_CMND_HVT_SSHK_DCTT(soDinhDanhCaNhan, soCMND, hoVaTen, soSoHoKhau, tinhId, quanHuyenId, phuongXaId, diaChi, tuNgayPhamToi, denNgayPhamToi, banAnSo, tuNgayToaAn, denNgayToaAn, trangThai);
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return count;
	}

	public List<Object[]> findBySDDCN_CMND_HVT_SSHK_DCTT(
			String soDinhDanhCaNhan, String soCMND, String hoVaTen,
			String soSoHoKhau, long tinhId, long quanHuyenId, long phuongXaId,
			String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi, String banAnSo, Date tuNgayToaAn, Date denNgayToaAn, int trangThai, int start, int end, OrderByComparator obc) {
		try {
			 return doiTuongTienAnFinder
					.findBySDDCN_CMND_HVT_SSHK_DCTT(soDinhDanhCaNhan, soCMND, hoVaTen, soSoHoKhau, tinhId, quanHuyenId, phuongXaId, diaChi, tuNgayPhamToi, denNgayPhamToi, banAnSo, tuNgayToaAn, denNgayToaAn, trangThai, start, end, obc);
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return null;
	}

	public int countBySSHK_DCTT(String soSoHoKhau, Date tuNgayPhamToi,
			Date denNgayPhamToi, long tinhId, long quanHuyenId,
			long phuongXaId, String diaChi) throws SystemException {
		return doiTuongTienAnFinder.countBySSHK_DCTT(soSoHoKhau, tuNgayPhamToi,
				denNgayPhamToi, tinhId, quanHuyenId, phuongXaId, diaChi);
	}
	public List<HoGiaDinhDTO> findDsHoGiaDinhByVungDiaLy_ThoiGianToaAn(long tinhId, long quanHuyenId, long phuongXaId, Date tuNgayToaAn, Date denNgayToaAn, int start, int end, OrderByComparator order) throws SystemException{
		List<Object[]> list = doiTuongTienAnFinder.findDSHoGiaDinhByVungDiaLy_ThoiGianToaAn(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn,start,end,order);
		List<HoGiaDinhDTO> result = new ArrayList<HoGiaDinhDTO>();
		for (int i = 0; i < list.size(); i++) {
			Object[] objDoiTuong = list.get(i);
			HoGiaDinhDTO hoGiaDinh = new HoGiaDinhDTO();
			long diaChiThuongTruTinhId = Long.parseLong(objDoiTuong[0].toString());
			long diaChiThuongTruQuanHuyenId = Long.parseLong(objDoiTuong[1].toString());
			long diaChiThuongTruPhuongXaId = Long.parseLong(objDoiTuong[2].toString());
			String diaChiThuongTru = objDoiTuong[3].toString();
			String diaChi = DiaChiUtils.createBlockDiaChi(diaChiThuongTruTinhId, diaChiThuongTruQuanHuyenId, diaChiThuongTruPhuongXaId, diaChiThuongTru);
			hoGiaDinh.setDiaChiThuongTru(diaChi);
			hoGiaDinh.setSoSoHoKhau(objDoiTuong[4].toString());
			hoGiaDinh.setHoVaTenChuHo(objDoiTuong[5].toString());
			hoGiaDinh.setSoLuongNguoiPhamToi(Integer.parseInt(objDoiTuong[6].toString()));
			result.add(hoGiaDinh);
		}
		return result;
	}
	public int countDsHoGiaDinhByVungDiaLy_ThoiGianToaAn(long tinhId, long quanHuyenId, long phuongXaId, Date tuNgayToaAn, Date denNgayToaAn) throws SystemException{
		return doiTuongTienAnFinder.countDSHoGiaDinhByVungDiaLy_ThoiGianToaAn(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn);
	}
	public int countDsTienAnByVungDiaLy_ThoiGianToaAn(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, int xoaAnTich) throws SystemException{
		return doiTuongTienAnFinder.countDsTienAnByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, xoaAnTich);
	}

	public List<DS_NguoiTienAnDTO> findDsTienAnByVungDiaLy_ThoiGianToaAn(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, int xoaAnTich, int start, int end, OrderByComparator order)
			throws SystemException {
		try {
			List<DS_NguoiTienAnDTO> DanhSachTienAns = new ArrayList<DS_NguoiTienAnDTO>();

			List<Object[]> objects = doiTuongTienAnFinder.findDsTienAnByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, xoaAnTich,start,end,order);

			int length = (objects != null ? objects.size() : 0);

			if (length > 0) {
				Object[] objDoiTuong = null;

				DS_NguoiTienAnDTO doiTuongTienAn = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					doiTuongTienAn = new DS_NguoiTienAnDTO();
					doiTuongTienAn.setHoTen(objDoiTuong[4] != null ? objDoiTuong[4].toString() : "");
					doiTuongTienAn.setSoDinhDanhCaNhan(objDoiTuong[5] != null ? objDoiTuong[5].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(Long.parseLong(objDoiTuong[0].toString()),Long.parseLong(objDoiTuong[1].toString()),Long.parseLong(objDoiTuong[2].toString()),objDoiTuong[3].toString());
					doiTuongTienAn.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
					doiTuongTienAn.setSoCMND(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					int soLanPhamToi = objDoiTuong[9] != null ? Integer.valueOf(objDoiTuong[9].toString()) : 0;
					String toiDanh = objDoiTuong[7] != null ? objDoiTuong[7].toString() : "";

					doiTuongTienAn.setToiDanh(toiDanh);

					if (objDoiTuong[10] != null) {
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						doiTuongTienAn.setNgaySinh(ngaySinh);
					}

					int trangThai = TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue();

					TienAn tienAnMoiNhatDangThiHanhAn = layTienAnDangThiHanhAnCuaDoiTuong(doiTuongTienAn.getSoDinhDanhCaNhan());

					boolean daXoaAnTich = Validator.isNull(tienAnMoiNhatDangThiHanhAn);

					if (daXoaAnTich) {
						trangThai = TrangThaiTienAnTrongThongKeEnum.DA_XOA_AN_TICH.getValue();
					} else {
						doiTuongTienAn.setNgayToaAnMoiNhat(tienAnMoiNhatDangThiHanhAn.getNgayToaAn());
					}

					doiTuongTienAn.setTrangThai(trangThai);
					doiTuongTienAn.setSoLanPhamToi(soLanPhamToi);

					if (xoaAnTich == TrangThaiTienAnTrongThongKeEnum.TAT_CA.getValue()) {
						DanhSachTienAns.add(doiTuongTienAn);
					} else if (trangThai == TrangThaiTienAnTrongThongKeEnum.DA_XOA_AN_TICH.getValue() && xoaAnTich == TrangThaiTienAnTrongThongKeEnum.DA_XOA_AN_TICH.getValue()){
						DanhSachTienAns.add(doiTuongTienAn);
					} else if (trangThai == TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue() && xoaAnTich == TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue()){
						DanhSachTienAns.add(doiTuongTienAn);
					}
				}

				return DanhSachTienAns;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<DS_NguoiTienAnDTO>();
		}

		return  new ArrayList<DS_NguoiTienAnDTO>();
	}

	private boolean isContain(String[] list, String item){
		if (Validator.isNull(list)) {
			return false;
		}
		for (String str : list){
			System.out.println("str > "+str+" item "+item);
			if (str.equals(item)) {
				return true;
			}
		}
		return false;
	}

	public List<DS_NguoiTienAnDTO> createDataForReportDanhSachDoiTuongCanXoaAnTich(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, String[] soDinhDanhCaNhans)
			{
		try {

			List<Object[]> objects = doiTuongTienAnFinder.findDsTienAnByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, TrangThaiTienAnEnum.DANG_THI_HANH_AN.getValue(),-1,-1,null);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
				List<DS_NguoiTienAnDTO> DanhSachTienAns = new ArrayList<DS_NguoiTienAnDTO>();

				for (int i = 0; i < length; i++) {
					Object[] objDoiTuong  = objects.get(i);
					String soDinhDanhCaNhan = objDoiTuong[5] != null ? objDoiTuong[5].toString() : "";
					DS_NguoiTienAnDTO nguoiTienAnDTO = new DS_NguoiTienAnDTO();

					if (isContain(soDinhDanhCaNhans, soDinhDanhCaNhan)){
					nguoiTienAnDTO.setHoTen(objDoiTuong[4] != null ? objDoiTuong[4].toString() : "");
					nguoiTienAnDTO.setSoDinhDanhCaNhan(soDinhDanhCaNhan);
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(Long.parseLong(objDoiTuong[0].toString()),Long.parseLong(objDoiTuong[1].toString()),Long.parseLong(objDoiTuong[2].toString()),objDoiTuong[3].toString());
					nguoiTienAnDTO.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
					nguoiTienAnDTO.setSoCMND(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					int soLanPhamToi = objDoiTuong[9] != null ? Integer.valueOf(objDoiTuong[9].toString()) : 0;
					String toiDanh = objDoiTuong[7] != null ? objDoiTuong[7].toString() : "";

					nguoiTienAnDTO.setToiDanh(toiDanh);

					if (objDoiTuong[10] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						nguoiTienAnDTO.setNgaySinh(ngaySinh);
					}
					List<TienAn> tienAnDangThiHanhAns = layDanhSachTienAnDangThiHanhAnCuaDoiTuong(nguoiTienAnDTO.getSoDinhDanhCaNhan());

					nguoiTienAnDTO.setTrangThai(TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue());
					nguoiTienAnDTO.setSoLanPhamToi(soLanPhamToi);
						for (TienAn ts : tienAnDangThiHanhAns){
							DS_NguoiTienAnDTO d1 = nguoiTienAnDTO.clone(nguoiTienAnDTO);
							d1.setNgayToaAn(ts.getNgayToaAn());
							d1.setToiDanh(ts.getToiDanh());
							d1.setToaAnPhat(ts.getToaAnXuPhat());
							d1.setHinhPhat(ts.getHinhPhatChinh());
							d1.setNewDoiTuong(false);

							if (!isContain(DanhSachTienAns, d1)){
								int stt = i+1;
								d1.setStt(stt);
								d1.setNewDoiTuong(true);
							}
							DanhSachTienAns.add(d1);
						}
					}
				}

				return DanhSachTienAns;
		} catch (Exception e) {
			System.out.println("E > "+e.getMessage());
			return null;
		}

	}
	public List<DS_NguoiTienAnDTO> createDataForReportDanhSachDoiTuongCanXoaAnTichExpandDiv(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, String[] soDinhDanhCaNhans, String[] tienAnIds)
			{
		try {

			List<Object[]> objects = doiTuongTienAnFinder.findDsTienAnByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, TrangThaiTienAnEnum.DANG_THI_HANH_AN.getValue(),-1,-1,null);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
				List<DS_NguoiTienAnDTO> DanhSachTienAns = new ArrayList<DS_NguoiTienAnDTO>();

				for (int i = 0; i < length; i++) {
					Object[] objDoiTuong  = objects.get(i);
					String soDinhDanhCaNhan = objDoiTuong[5] != null ? objDoiTuong[5].toString() : "";
					DS_NguoiTienAnDTO nguoiTienAnDTO = new DS_NguoiTienAnDTO();

					if (isContain(soDinhDanhCaNhans, soDinhDanhCaNhan)){
					nguoiTienAnDTO.setHoTen(objDoiTuong[4] != null ? objDoiTuong[4].toString() : "");
					nguoiTienAnDTO.setSoDinhDanhCaNhan(soDinhDanhCaNhan);
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(Long.parseLong(objDoiTuong[0].toString()),Long.parseLong(objDoiTuong[1].toString()),Long.parseLong(objDoiTuong[2].toString()),objDoiTuong[3].toString());
					nguoiTienAnDTO.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
					nguoiTienAnDTO.setSoCMND(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					int soLanPhamToi = objDoiTuong[9] != null ? Integer.valueOf(objDoiTuong[9].toString()) : 0;
					String toiDanh = objDoiTuong[7] != null ? objDoiTuong[7].toString() : "";

					nguoiTienAnDTO.setToiDanh(toiDanh);

					if (objDoiTuong[10] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						nguoiTienAnDTO.setNgaySinh(ngaySinh);
					}

					nguoiTienAnDTO.setTrangThai(TrangThaiTienAnTrongThongKeEnum.DANG_THI_HANH_AN.getValue());
					nguoiTienAnDTO.setSoLanPhamToi(soLanPhamToi);
						for (String tienAnId: tienAnIds){
							System.out.println("tienAnId >>>>>>>>>>>>>>>>>>>"+tienAnId);
							TienAn ts = tienAnLocalService.findByTienAnId(Long.parseLong(tienAnId));
							if(Validator.isNotNull(ts)){
								DoiTuongTienAn doiTuong = doiTuongTienAnLocalService.findByDoiTuongTienAnId(ts.getDoiTuongTienAnId());
								if(Validator.isNotNull(doiTuong)){
									if (doiTuong.getSoDinhDanhCaNhan().equals(soDinhDanhCaNhan)){
										DS_NguoiTienAnDTO d1 = nguoiTienAnDTO.clone(nguoiTienAnDTO);
										d1.setNgayToaAn(ts.getNgayToaAn());
										d1.setToiDanh(ts.getToiDanh());
										d1.setToaAnPhat(ts.getToaAnXuPhat());
										d1.setHinhPhat(ts.getHinhPhatChinh());
										d1.setNewDoiTuong(false);
										System.out.println("d1 >>>>>>>>>>>>>>>>>>>>>,"+d1.getSoDinhDanhCaNhan());
										if (!isContain(DanhSachTienAns, d1)){
											int stt = i+1;
											d1.setStt(stt);
											d1.setNewDoiTuong(true);
										}
										DanhSachTienAns.add(d1);
									}
								}
							}
						}
					}

				}

				return DanhSachTienAns;
		} catch (Exception e) {
			System.out.println("E > "+e.getMessage());
			return null;
		}

	}

	private boolean isContain(List<DS_NguoiTienAnDTO> danhSachTienAns,
			DS_NguoiTienAnDTO nguoiTienAnDTO) {
		for (DS_NguoiTienAnDTO obj : danhSachTienAns){
			if (obj.getSoDinhDanhCaNhan().equals(nguoiTienAnDTO.getSoDinhDanhCaNhan())) {
				return true;
			}
		}
		return false;
	}
	public int countByDS_XoaAnTich(long tinhId,
			long quanHuyenId, long phuongXaId, Date tuNgayXoaAnTich, Date denNgayXoaAnTich)
			throws SystemException {
		return doiTuongTienAnFinder.countByDS_XoaAnTich(tinhId, quanHuyenId, phuongXaId, tuNgayXoaAnTich, denNgayXoaAnTich);

	}
	public List<DS_XoaAnTichDTO> findByDS_XoaAnTich(long tinhId,
			long quanHuyenId, long phuongXaId, Date tuNgayXoaAnTich, Date denNgayXoaAnTich,  int start, int end, OrderByComparator order)
			throws SystemException {

		try {
			sdf = new SimpleDateFormat("dd/MM/yyyy");
			List<Object[]> objects = doiTuongTienAnFinder.findByDS_XoaAnTich(
					 tinhId,
					quanHuyenId, phuongXaId, tuNgayXoaAnTich, denNgayXoaAnTich,start,end,order);

			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_XoaAnTichDTO> danhSachXoaAnTichs = new ArrayList<DS_XoaAnTichDTO>();

				Object[] objDoiTuong = null;
				DS_XoaAnTichDTO danhSachXoaAnTich = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					danhSachXoaAnTich = new DS_XoaAnTichDTO();
					danhSachXoaAnTich
							.setSoSoHoKhau(objDoiTuong[0] != null ? objDoiTuong[0]
									.toString() : "");
					danhSachXoaAnTich
							.setHoTen(objDoiTuong[5] != null ? objDoiTuong[5]
									.toString() : "");
					danhSachXoaAnTich
							.setSoDinhDanhCaNhan(objDoiTuong[6] != null ? objDoiTuong[6]
									.toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					danhSachXoaAnTich
							.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru
									: "");
					danhSachXoaAnTich
							.setSoCMND(objDoiTuong[7] != null ? objDoiTuong[7]
									.toString() : "");

					int soLanPhamToi = objDoiTuong[10] != null ? Integer
							.valueOf(objDoiTuong[10].toString()) : 0;
					String toiDanh = objDoiTuong[8] != null ? objDoiTuong[8].toString() : "";
					if(soLanPhamToi <= 1) {
						toiDanh = toiDanh.substring(0, toiDanh.indexOf(","));
					}

					danhSachXoaAnTich.setToiDanh(toiDanh);

					String ngayPhamToi = objDoiTuong[9] != null ? objDoiTuong[9].toString() : "";
					if(soLanPhamToi <= 1) {
						ngayPhamToi = ngayPhamToi.substring(0, 9);
					}

					danhSachXoaAnTich.setNgayPhamToi(ngayPhamToi);

					if (objDoiTuong[11] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[11].toString());
						danhSachXoaAnTich.setNgaySinh(ngaySinh);
					}

					danhSachXoaAnTich
							.setSoLanPhamToi(soLanPhamToi);
					if (objDoiTuong[12] != null){
						danhSachXoaAnTich.setGioiTinh(Integer.parseInt(objDoiTuong[12].toString()));
					}

					/*String soDinhDanhCaNhan = objDoiTuong[6] != null ? objDoiTuong[6]
							.toString() : "";
					DoiTuongTienAn doiTuongTienAn = DoiTuongTienAnLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
					StringBuffer ngayXoaAnTich = new StringBuffer();		 
					List<Date> ngayXoaAnTichs = LichSuXoaAnTichLocalServiceUtil.findByNgayKyDuyetXoaAnTich(tuNgayXoaAnTich,
							denNgayXoaAnTich,doiTuongTienAn.getDoiTuongTienAnId());
					if(ngayXoaAnTichs.size()<2) {
						ngayXoaAnTich.append(sdf.format(ngayXoaAnTichs.get(0)));
					} else {
						for( Date s : ngayXoaAnTichs) {
							ngayXoaAnTich.append(sdf.format(s) + ",");
						}
					}

					
						danhSachXoaAnTich.setNgayXoaAnTich(ngayXoaAnTich.toString());*/
					

					danhSachXoaAnTichs.add(danhSachXoaAnTich);

				}

				return danhSachXoaAnTichs;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	public int countByLichSuPhamToi(String soDinhDanhCaNhan, String hoVaTen,
			long tinhId, long quanHuyenId, long phuongXaId, String diaChi)
			throws SystemException {
		return doiTuongTienAnFinder.countByLichSuPhamToi(soDinhDanhCaNhan,
				hoVaTen, tinhId, quanHuyenId, phuongXaId, diaChi);
	}

	public List<DS_LichSuPhamToiDTO> findByLichSuPhamToi(
			String soDinhDanhCaNhan, String hoVaTen, long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi, int start, int end)
			throws SystemException {

		try {
			List<Object[]> objects = doiTuongTienAnFinder.findByLichSuPhamToi(
					soDinhDanhCaNhan, hoVaTen, tinhId, quanHuyenId, phuongXaId,
					diaChi, start, end);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_LichSuPhamToiDTO> DanhSachXoaAnTichs = new ArrayList<DS_LichSuPhamToiDTO>();

				Object[] objDoiTuong = null;
				DS_LichSuPhamToiDTO danhSachLichSuPhamToi = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					danhSachLichSuPhamToi = new DS_LichSuPhamToiDTO();
					danhSachLichSuPhamToi
							.setDoiTuongTienAnId(objDoiTuong[0] != null ? Long
									.valueOf(objDoiTuong[0].toString()) : 0L);
					danhSachLichSuPhamToi
							.setHoVaTen(objDoiTuong[5] != null ? objDoiTuong[5]
									.toString() : "");
					danhSachLichSuPhamToi
							.setSoDinhDanhCaNhan(objDoiTuong[6] != null ? objDoiTuong[6]
									.toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					danhSachLichSuPhamToi
							.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru
									: "");

					danhSachLichSuPhamToi
							.setSoLanPhamToi(objDoiTuong[7] != null ? Integer
									.valueOf(objDoiTuong[7].toString()) : 0);

					DanhSachXoaAnTichs.add(danhSachLichSuPhamToi);

				}

				return DanhSachXoaAnTichs;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public int countBySoSoHoKhau(long tinhId, long quanHuyenId,
			long phuongXaId, String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi) throws SystemException {
		return doiTuongTienAnFinder.countBySoHoKhau(tinhId, quanHuyenId,
				phuongXaId, diaChi, tuNgayPhamToi, denNgayPhamToi);
	}

	public List<SoHoKhauDTO> findBySoSoHoKhau(long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi,
			Date tuNgayPhamToi, Date denNgayPhamToi, int start, int end)
			throws SystemException {

		try {
			List<Object[]> objects = doiTuongTienAnFinder.findBySoHoKhau(tinhId, quanHuyenId, phuongXaId,
					diaChi, tuNgayPhamToi, denNgayPhamToi, start, end);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<SoHoKhauDTO> dsSoHoKhau = new ArrayList<SoHoKhauDTO>();

				Object[] objDoiTuong = null;
				SoHoKhauDTO soHoKhau = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					soHoKhau = new SoHoKhauDTO();
					soHoKhau.setSoSoHokhau(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					soHoKhau.setDiaChi(diaChiThuongTru);

					dsSoHoKhau.add(soHoKhau);

				}

				return dsSoHoKhau;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public List<DS_HoGiaDinhDTO> findByDS_HoGiaDinh(String soSoHoKhau)
			throws SystemException {

		try {
			List<Object[]> objects = doiTuongTienAnFinder.findByDS_HoGiaDinh(soSoHoKhau);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_HoGiaDinhDTO> dsNguoiCoTienAn = new ArrayList<DS_HoGiaDinhDTO>();

				Object[] objDoiTuong = null;
				DS_HoGiaDinhDTO nguoiCoTienAn = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					nguoiCoTienAn = new DS_HoGiaDinhDTO();

					nguoiCoTienAn
							.setHoVaTen(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					nguoiCoTienAn
					.setNgaySinh((Date) (objDoiTuong[1] != null ? objDoiTuong[1] : null));

					String gioiTinh = "";
					if (objDoiTuong[2] != null) {
						gioiTinh = objDoiTuong[2].toString().equalsIgnoreCase("0") ? "Nam" : "Nu";
					}

					nguoiCoTienAn
					.setGioiTinh(gioiTinh);
					nguoiCoTienAn
					.setSoCMND(objDoiTuong[3] != null ? objDoiTuong[3].toString() : "");
					String quanHe = "";
					if (objDoiTuong[4] != null) {

					QuanHeGiaDinh quanHeGiaDinh = QuanHeGiaDinhLocalServiceUtil.findById(Long.valueOf(objDoiTuong[4].toString()));
					quanHe = quanHeGiaDinh.getTenQuanHeGiaDinh();
					}

					nguoiCoTienAn
					.setQuanHeVoiChuHo(quanHe);


					int soLanPhamToi = objDoiTuong[9] != null ? Integer
							.valueOf(objDoiTuong[9].toString()) : 0;

					String toiDanh = objDoiTuong[6] != null ? objDoiTuong[6].toString() : "";
					toiDanh = toiDanh.substring(0, toiDanh.lastIndexOf(","));
					if(soLanPhamToi > 1) {
						toiDanh = toiDanh.substring(0, toiDanh.lastIndexOf(","));
					}
					nguoiCoTienAn.setToiDanh(toiDanh);

					String ngayPhamToi = (objDoiTuong[5] != null ? objDoiTuong[5].toString() : "");
					ngayPhamToi = ngayPhamToi.substring(0, ngayPhamToi.lastIndexOf(","));
					/*if(soLanPhamToi > 1) {
						ngayPhamToi = ngayPhamToi.substring(0, ngayPhamToi.lastIndexOf(","));
					}*/
					nguoiCoTienAn
					.setNgayPhamToi(ngayPhamToi);

					String hinhPhat = (objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");
					hinhPhat = hinhPhat.substring(0, hinhPhat.lastIndexOf(","));
					if(soLanPhamToi > 1) {
						hinhPhat = hinhPhat.substring(0, hinhPhat.lastIndexOf(","));
					}
					nguoiCoTienAn
					.setHinhPhat(objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");

					String ghiChu = (objDoiTuong[8] != null ? objDoiTuong[8].toString() : "");
					ghiChu = ghiChu.substring(0, ghiChu.lastIndexOf(","));
					if(soLanPhamToi > 1) {
						ghiChu = ghiChu.substring(0, ghiChu.lastIndexOf(","));
					}


					nguoiCoTienAn
					.setGhiChu(ghiChu);

					dsNguoiCoTienAn.add(nguoiCoTienAn);

				}

				return dsNguoiCoTienAn;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}


	public int countByDS_TaiPham(long tinhId, long quanHuyenId,
			long phuongXaId, String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi) throws SystemException {
		return doiTuongTienAnFinder.countByDS_TaiPham(tuNgayPhamToi, denNgayPhamToi, tinhId, quanHuyenId,
				phuongXaId, diaChi);
	}
	public void xoaAnTichDoiTuongTienAn(long doiTuongTienAnId) throws SystemException{
		List<TienAn> tienAn = TienAnLocalServiceUtil.findByDoiTuongTienAnId(doiTuongTienAnId);
		if (tienAn.size() == 0){
			DoiTuongTienAn dtts = findByDoiTuongTienAnId(doiTuongTienAnId);
			if (Validator.isNotNull(dtts)){
				dtts.setXoaAnTich(true);
				updateDoiTuongTienAn(dtts);
			}
		}
	}

	public List<DS_TaiPhamDTO> findByDS_TaiPham(long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi,
			Date tuNgayPhamToi, Date denNgayPhamToi, int start, int end)
			throws SystemException {
		try {
			List<Object[]> objects = doiTuongTienAnFinder.findByDS_TaiPham(tuNgayPhamToi, denNgayPhamToi, tinhId, quanHuyenId, phuongXaId,
					diaChi, start, end);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_TaiPhamDTO> dsTaiPham = new ArrayList<DS_TaiPhamDTO>();

				Object[] objDoiTuong = null;
				DS_TaiPhamDTO taiPham = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					taiPham = new DS_TaiPhamDTO();
					taiPham.setSoSoHoKhau(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					taiPham.setDiaChi(diaChiThuongTru);
					taiPham.setHoVaTen(objDoiTuong[5] != null ? objDoiTuong[5].toString() : "");
					taiPham.setSoDinhDanhCaNhan(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					taiPham.setSoCMND(objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");
					taiPham.setNgayPhamToi(objDoiTuong[8] != null ? objDoiTuong[8].toString() : "");
					taiPham.setToiDanh(objDoiTuong[9] != null ? objDoiTuong[9].toString() : "");
					taiPham.setToiDanhTruoc(objDoiTuong[10] != null ? objDoiTuong[10].toString() : "");
					taiPham.setNgayPhamToiTruoc(objDoiTuong[11] != null ? objDoiTuong[11].toString() : "");
					taiPham.setNgayXoaAnTich(objDoiTuong[12] != null ? objDoiTuong[12].toString() : "");
					taiPham.setNhanXet(objDoiTuong[13] != null ? objDoiTuong[13].toString() : "");

					dsTaiPham.add(taiPham);

				}

				return dsTaiPham;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}
	
	public Date findLatestNgayToaAn_bySoHoKhau(String soHoKhau)
			throws SystemException {
		try{
		return doiTuongTienAnFinder.findLatestNgayToaAn_bySoHoKhau(soHoKhau);
		} catch (Exception e) {
			 return null;
			 
		}
	}
}
